﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace L.EntityFramework
{
    /// <summary>
    /// 数据访问基类接口
    /// </summary>
    /// <typeparam name="TC"></typeparam>
    public interface IBaseRepository<TC> where TC : DbContext, new()
    {
        #region 数据插入

        /// <summary>
        /// 新增数据
        /// 通过EntityFramework
        /// </summary>
        int Insert<T>(T t) where T : BaseEntity;

        /// <summary>
        /// 新增数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> InsertAsync<T>(T t) where T : BaseEntity;

        /// <summary>
        /// 新增数据
        /// 通过EntityFramework
        /// </summary>
        int Insert<T>(IEnumerable<T> t) where T : BaseEntity;


        /// <summary>
        /// 新增数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> InsertAsync<T>(IEnumerable<T> t) where T : BaseEntity;

        /// <summary>
        /// 新增数据
        /// 采用SQL方式
        /// 通过ADO.NET
        /// </summary>
        int Insert<T>(string sql, MySqlParameter[] parameters = null) where T : BaseEntity;

        /// <summary>
        /// 新增数据
        /// 采用SQL方式
        /// 通过ADO.NET
        /// </summary>
        Task<int> InsertAsync<T>(string sql, MySqlParameter[] parameters = null) where T : BaseEntity;

        /// <summary>
        /// 新增数据
        /// 采用转译SQL方式
        /// 通过ADO.NET
        /// </summary>
        int InsertAdoptSql<T>(T t) where T : BaseEntity;

        #endregion

        #region 更新

        /// <summary>
        /// 更新数据
        /// 通过EntityFramework
        /// </summary>
        int Update<T>(T t) where T : BaseEntity;
        /// <summary>
        /// 更新数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> UpdateAsync<T>(T t) where T : BaseEntity;

        /// <summary>
        /// 更新数据
        /// 通过EntityFramework
        /// </summary>
        int Update<T>(IEnumerable<T> t) where T : BaseEntity;
        /// <summary>
        /// 更新数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> UpdateAsync<T>(IEnumerable<T> t) where T : BaseEntity;

        /// <summary>
        /// 更新数据
        /// 通过Lambda、Action、EntityFramework
        /// </summary>
        int Update<T>(Expression<Func<T, bool>> expression, Action<T> action) where T : BaseEntity;

        /// <summary>
        /// 更新数据
        /// 通过Lambda、Action、EntityFramework
        /// </summary>
        Task<int> UpdateAsync<T>(Expression<Func<T, bool>> expression, Action<T> action) where T : BaseEntity;

        /// <summary>
        /// 更新数据
        /// 直接执行SQL语句
        /// 通过ADO.NET
        /// </summary>
        int Update(string sql);

        /// <summary>
        /// 更新数据
        /// 直接执行SQL语句
        /// 通过ADO.NET
        /// </summary>
        Task<int> UpdateAsync(string sql, MySqlParameter[] parameters = null);


        /// <summary>
        /// 更新数据
        /// 通过ADO.NET
        /// </summary>
        int UpdateAdoptSql<T>(T t) where T : BaseEntity;

        #endregion

        #region 删除

        /// <summary>
        /// 删除数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> DeleteAsync<T>(T t) where T : BaseEntity;
        /// <summary>
        /// 删除数据
        /// 通过EntityFramework
        /// </summary>
        int Delete<T>(T t) where T : BaseEntity;

        /// <summary>
        /// 删除数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> DeleteAsync<T>(IEnumerable<T> t) where T : BaseEntity;
        /// <summary>
        /// 删除数据
        /// 通过EntityFramework
        /// </summary>
        int Delete<T>(IEnumerable<T> t) where T : BaseEntity;

        /// <summary>
        /// 删除数据
        /// 通过EntityFramework
        /// </summary>
        Task<int> DeleteAsync<T>(Expression<Func<T, bool>> expression) where T : BaseEntity;

        /// <summary>
        /// 删除数据
        /// 通过EntityFramework
        /// </summary>
        int Delete<T>(Expression<Func<T, bool>> expression) where T : BaseEntity;

        #endregion

        #region 查询

        /// <summary>
        /// 数据查询
        /// EntityFramework
        /// </summary>
        IQueryable<T> Search<T>(Expression<Func<T, bool>> expression = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// EntityFramework
        /// </summary>
        Task<IQueryable<T>> SearchAsync<T>(Expression<Func<T, bool>> expression = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// EntityFramework
        T SearchFirstOrDefault<T>(Expression<Func<T, bool>> expression = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// EntityFramework
        Task<T> SearchFirstOrDefaultAsync<T>(Expression<Func<T, bool>> expression = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// ADO.NET
        T SearchFirstOrDefault<T>(string sql, MySqlParameter[] paramer = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// ADO.NET
        Task<T> SearchFirstOrDefaultAsync<T>(string sql, MySqlParameter[] paramer = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// EntityFramework
        List<T> SearchList<T>(Expression<Func<T, bool>> expression = null) where T : BaseEntity;

        Task<List<T>> SearchListAsync<T>(Expression<Func<T, bool>> expression = null) where T : BaseEntity;

        /// <summary>
        /// 数据查询
        /// ADO.NET
        List<T> SearchList<T>(string sql) where T : BaseEntity;
        /// <summary>
        /// 数据查询
        /// ADO.NET
        Task<List<T>> SearchListAsync<T>(string sql) where T : BaseEntity;

        #endregion

        #region ADO.NET


        /// <summary>
        /// 判断表是否存在
        /// ADO.NET
        /// </summary>
        bool TableExist(string tableName);

        /// <summary>
        /// 执行存储过程
        /// ADO.NET
        /// </summary>
        T ExecuteStoredProcedure<T>(string procedureName, MySqlParameter[] parameter = null) where T : BaseEntity;

        #endregion
    }
}
